package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public class RoomBeanLabelAdapter extends AppAdapter<RoomBean> {
    private Map<String, String> mSelectTitle = new LinkedHashMap<>();
    private FloorBean floorBean;
    private boolean hasAllItem = false;
    private boolean mAllItemState = false;
    public static final long ALL_ROOM_ID = -9999;//所有房间
    private boolean canBeEmpty = false;
    private Map<String, RoomBean> selected = new LinkedHashMap<>();//所有楼层被选择的房间
    private Map<String, RoomBean> curFloorSelected = new LinkedHashMap<>();//当前楼层被选择的房间
    public RoomBeanLabelAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTextView;

        private ViewHolder() {
            super(R.layout.room_label_item);
            mTextView = findViewById(R.id.tv_room);
            mTextView.setOnClickListener(mOnClickListener);
        }

        @Override
        public void onBindView(int position) {
            mTextView.setText(getItem(position).getRoomName());
            mTextView.setTag(getItem(position));
            if (isAllItemPosition(position)){
                mTextView.setSelected(mAllItemState);
            }else {
                mTextView.setSelected(selected.containsKey(getItem(position).getRoomSerialNo()));
            }
        }
    }

    private boolean isAllItemPosition(int position) {
        if (getItem(position).getId() == null) return false;
        if (getItem(position).getId() == ALL_ROOM_ID){
            return true;
        }else {
            return false;
        }
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RoomBean data = (RoomBean) v.getTag();
            if (canBeEmpty){
                v.setSelected(!v.isSelected());
            }else {
                if (selected.size()==1 && selected.get(data.getRoomSerialNo())!=null){
                    v.setSelected(true);
                    return;
                }else {
                    v.setSelected(!v.isSelected());
                }
            }
            if (data.getId() != null && data.getId() == ALL_ROOM_ID){//所有房间
                dealAllItems(v.isSelected());
            }else {
                if (v.isSelected()) {
                    curFloorSelected.put(data.getRoomSerialNo(), data);
                    selected.put(data.getRoomSerialNo(), data);
                } else {
                    curFloorSelected.remove(data.getRoomSerialNo());
                    selected.remove(data.getRoomSerialNo());
                }
                checkAllItemState();
            }
            if(floorBean != null){
                if (mAllItemState){
                    mSelectTitle.put(floorBean.getFloorSerialNo(), floorBean.getFloorName()+"所有");
                }else {
                    StringBuilder titleStr = new StringBuilder();
                    for (RoomBean bean : curFloorSelected.values()){
                        if (titleStr.length() == 0) {
                            titleStr.append(bean.getRoomName());
                        } else {
                            titleStr.append("、").append(bean.getRoomName());
                        }
                    }
                    mSelectTitle.put(floorBean.getFloorSerialNo(), titleStr.toString());
                }
            }
            onSelectChanged(selected);
        }
    };

    private void checkAllItemState() {
        if (hasAllItem){
            if (mAllItemState != (getData().size()==curFloorSelected.size()+1)){
                mAllItemState = !mAllItemState;
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 处理所有项
     * @param select
     */
    public void dealAllItems(boolean select){
        mAllItemState = select;
        for (int i = 0; i < getData().size(); i++) {
            RoomBean data = getData().get(i);
            if (data.getId() != null && data.getId() == ALL_ROOM_ID){
                continue;
            }
            if (select){
                selected.put(data.getRoomSerialNo(), data);
                curFloorSelected.put(data.getRoomSerialNo(), data);
            }else {
                selected.remove(data.getRoomSerialNo());
                curFloorSelected.remove(data.getRoomSerialNo());
            }
        }
        notifyDataSetChanged();
    }


    public ArrayList<RoomBean> getSelectedData() {
        ArrayList<RoomBean> checkList = new ArrayList<>();
        for(String key : selected.keySet()){
            checkList.add(selected.get(key));
        }
        return checkList;
    }

    public void setSelected(List<RoomBean> selected){
        if(selected != null){
            for(RoomBean data : selected){
                this.selected.put(data.getRoomSerialNo(), data);
            }
        }
    }

    public void setSelected(Map<String, RoomBean> selected){
        this.selected = selected;
    }

    /**
     * 头部带有所有选项。
     * @param data
     * @param totalSelected
     */
    public void setDataWithHeadAllItem(FloorBean data, Map<String, RoomBean> totalSelected, Map<String, String> selectAreaTitle){
        floorBean = data;
        hasAllItem = true;
        mSelectTitle = selectAreaTitle;
        this.selected = totalSelected;
        curFloorSelected.clear();
        for (RoomBean bean : data.getRooms()){
            if (selected.containsKey(bean.getRoomSerialNo())){
                curFloorSelected.put(bean.getRoomSerialNo(), bean);
            }
        }
        mAllItemState = (data.getRooms().size() == curFloorSelected.size());
        RoomBean allItem = new RoomBean();
        allItem.setRoomName("所有");
        allItem.setId(ALL_ROOM_ID);
        List<RoomBean> list = new ArrayList<>();
        list.add(allItem);
        list.addAll(data.getRooms());
        setData(list);
    }

    public void clearSelected(){
        selected.clear();
    }

    public void setCanBeEmpty(boolean value){
        canBeEmpty = value;
    }

    //外部实现
    public void onSelectChanged(Map<String, RoomBean> selectBean){}

}